package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKHocVientblDKMHDotDKHocVienChiTietUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private int dotDKHocVienId; 
    public void setDotDKHocVienId(int dotDKHocVienId) {
       this.dotDKHocVienId = dotDKHocVienId;
 }

    public int getDotDKHocVienId() {
       return dotDKHocVienId;
 }

    private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService; 

    private String ngayBatDau; 
    public void setNgayBatDau(String ngayBatDau) {
       this.ngayBatDau = ngayBatDau;
 }

    public String getNgayBatDau() {
       return ngayBatDau;
 }

    private String ngayKetThuc; 
    public void setNgayKetThuc(String ngayKetThuc) {
       this.ngayKetThuc = ngayKetThuc;
 }

    public String getNgayKetThuc() {
       return ngayKetThuc;
 }

    private String quyen; 
    public void setQuyen(String quyen) {
       this.quyen = quyen;
 }

    public String getQuyen() {
       return quyen;
 }

    private TblDKMHDotDKHocVienChiTiet tblDKMHDotDKHocVienChiTiet; 
    public void setTblDKMHDotDKHocVienChiTiet (TblDKMHDotDKHocVienChiTiet tblDKMHDotDKHocVienChiTiet) {
        this.tblDKMHDotDKHocVienChiTiet = tblDKMHDotDKHocVienChiTiet;
 }

    private TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService; 

    private List<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets; 
    public List<TblDKMHDotDKHocVienChiTiet>  getTblDKMHDotDKHocVienChiTiets() {
        return tblDKMHDotDKHocVienChiTiets; 
      } 

	private int preUpdate(Message msg, TblDKMHDotDKHocVienChiTiet obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblDKMHDotDKHocVienChiTiet obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_HocVien_ChiTiet", "sua")){ 
        return "noprivilege"; 
    }
        tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService) context.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient"); 
        tblDKMHDotDKHocVienService = (TblDKMHDotDKHocVienService) context.getBean("tbl_DKMH_DotDK_HocVienClient"); 

        msg = new Message(); 
        tblDKMHDotDKHocVienChiTiet = tblDKMHDotDKHocVienChiTietService.gettblDKMHDotDKHocVienChiTiet(new Long(id)); 
        tblDKMHDotDKHocVienChiTiet.setDotDKHocVienId(tblDKMHDotDKHocVienService.gettblDKMHDotDKHocVien(new Long(dotDKHocVienId))); 
        
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKHocVienChiTiet.setNgayBatDau(d); 
        
       dd = df.parse(ngayKetThuc); 
       c.setTime(dd);
       d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKHocVienChiTiet.setNgayKetThuc(d); 
        
        
        tblDKMHDotDKHocVienChiTiet.setQuyen(quyen); 
        preUpdate(msg,tblDKMHDotDKHocVienChiTiet); 
       if (msg.isPreMessage_b()) 
       { 
           tblDKMHDotDKHocVienChiTietService.updatetblDKMHDotDKHocVienChiTiet(tblDKMHDotDKHocVienChiTiet); 
           postUpdate(msg, tblDKMHDotDKHocVienChiTiet); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
        List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
       ConditionReport con = new ConditionReport(); 
        con.setCol("dotDKHocVienId.id"); 
        con.setValue1(new Long(dotDKHocVienId)); 
        con.setOperator("eq"); 
        cons.add(con); 
      tblDKMHDotDKHocVienChiTiets =       tblDKMHDotDKHocVienChiTietService.findtblDKMHDotDKHocVienChiTietByCondition(cons,pageSize, page); 
      pages = tblDKMHDotDKHocVienChiTietService.counttblDKMHDotDKHocVienChiTietByCondition(cons)/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&dotDKHocVienId="; 
      dataString += "&ngayBatDau="; 
      dataString += "&ngayKetThuc="; 
      dataString += "&quyen="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_DKMH_DotDK_HocVien_ChiTiet Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
